package com.qn.strategy.event;

import com.qn.strategy.ICouponDiscount;
import com.qn.strategy.eums.EnumPromotionType;
import com.qn.strategy.factory.PromotionTypeServiceStrategyFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * 直减
 *
 * @author Qning
 * @date 2021/09/09
 */
@Service
public class ZJCouponDiscount implements ICouponDiscount<Double>, InitializingBean {

    /**
     * 直减计算
     * 1. 使用商品价格减去优惠价格
     * 2. 最低支付金额1元
     */
    @Override
    public BigDecimal discountAmount(Double couponInfo, BigDecimal skuPrice) {
        BigDecimal discountAmount = skuPrice.subtract(new BigDecimal(couponInfo));
        if (discountAmount.compareTo(BigDecimal.ZERO) < 1) {
            return BigDecimal.ONE;
        }
        return discountAmount;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        PromotionTypeServiceStrategyFactory.register(EnumPromotionType.ZJ.getValue(), this);
    }
}
